﻿'功能說明：系統主網址下的主要內容區，分menu,Split及MainPage1,2011/3/8日修改,原從資源檔取得其他語言別的面頁名稱,為增強彈性修改從DB取得,但只限繁中.簡體及英文3種語文.
'建立人員: andy
'建立日期：2011/1/31
'****************************************************
Imports System.Threading
Imports System.Globalization
Imports System.Resources
Imports System.Data
Partial Class WebForm_Main_Menu
    Inherits BasePage

    Dim intLevel As Integer = 0
    
    Dim MenuType As String = System.Configuration.ConfigurationManager.AppSettings("MenuType")

    Protected Sub BuildTreeView()
        'Dim db As New SqlDataSource
        Dim strRootID As String = ""
        Dim Nodes As Web.UI.WebControls.TreeNodeCollection

        Dim dao As OAMS.DAO.MenuDAO = New OAMS.DAO.MenuDAO(dbName)
        Dim dt As DataTable = dao.GetMenuByGroupRights(0, UID, MenuType) ''選擇Menu''根目錄

        For i As Integer = 0 To dt.Rows.Count - 1
          
            Nodes = Me.TreeView1.Nodes
            ''第1層點
            AddNewNode(dt.Rows(i)("ProgramID").ToString, dt.Rows(i)("Name").ToString, dt.Rows(i)("URL").ToString, 0, dt.Rows(i)("ChnName").ToString, dt.Rows(i)("EngName").ToString, dt.Rows(i)("IsNewWin").ToString)
            BuildSubTreeView(dt.Rows(i)("ProgramID").ToString)
            strRootID = dt.Rows(i)("RootID").ToString()
            intLevel = intLevel + 1
        Next
        dt = Nothing
        dao = Nothing
    End Sub
    ''' <summary>
    ''' 第2層樹
    ''' </summary>
    ''' <param name="RootID"></param>
    ''' <remarks></remarks>

    Protected Sub BuildSubTreeView(ByVal RootID As String)

        Dim strRootID As String = ""
        Dim Nodes As Web.UI.WebControls.TreeNodeCollection
         Dim dao As OAMS.DAO.MenuDAO = New OAMS.DAO.MenuDAO(dbName)
        Dim dt As DataTable = dao.GetMenuByGroupRights(RootID, UID, MenuType) ''選擇Menu

        Nodes = Me.TreeView1.Nodes
        For i As Integer = 0 To dt.Rows.Count - 1
            strRootID = dt.Rows(i)("RootID").ToString()
            ''第3層的點
            AddNewNode(dt.Rows(i)("ProgramID").ToString, dt.Rows(i)("Name").ToString, dt.Rows(i)("URL").ToString, 2, dt.Rows(i)("ChnName").ToString, dt.Rows(i)("EngName").ToString, dt.Rows(i)("IsNewWin").ToString)
        Next
        dt = Nothing
        dao = Nothing
    End Sub
    ''' <summary>
    ''' 第3層的樹
    ''' </summary>
    ''' <param name="RootID"></param>
    ''' <param name="SubItem"></param>
    ''' <remarks></remarks>
    Protected Sub BuildLevel3Tree(ByVal RootID As String, ByVal SubItem As TreeNode)
        Dim strRootID As String = ""
        Dim Nodes As Web.UI.WebControls.TreeNodeCollection
      
        Dim dao As OAMS.DAO.MenuDAO = New OAMS.DAO.MenuDAO(dbName)
        Dim dt As DataTable = dao.GetMenuByGroupRights(RootID, UID, MenuType) ''選擇Menu

        Nodes = Me.TreeView1.Nodes
        For i As Integer = 0 To dt.Rows.Count - 1
            strRootID = dt.Rows(i)("RootID").ToString()
            AddLevel3Node(SubItem, dt.Rows(i)("ProgramID").ToString, dt.Rows(i)("Name").ToString, dt.Rows(i)("URL").ToString, 2, dt.Rows(i)("ChnName").ToString, dt.Rows(i)("EngName").ToString, dt.Rows(i)("IsNewWin").ToString)
        Next
        dt = Nothing
        dao = Nothing
    End Sub
    ''' <summary>
    ''' 第3層的點
    ''' </summary>
    ''' <param name="subItem"></param>
    ''' <param name="strMID"></param>
    ''' <param name="strName"></param>
    ''' <param name="strURL"></param>
    ''' <param name="intLevel11"></param>
    ''' <param name="ChnName"></param>
    ''' <param name="EngName"></param>
    ''' <param name="IsNewWin"></param>
    ''' <remarks></remarks>
    Private Sub AddLevel3Node(ByVal subItem As TreeNode, ByVal strMID As String, ByVal strName As String, ByVal strURL As String, ByVal intLevel11 As Integer, ByVal ChnName As String, ByVal EngName As String, ByVal IsNewWin As String)
        Dim NewNode As TreeNode
        Dim tmpTitle As String = strName
        NewNode = New TreeNode
        Try
            Select Case (GetUserLanguage().ToLower)
                Case "en-us"
                    NewNode.Text = EngName
                Case "zh-cn"
                    NewNode.Text = ChnName
                Case Else
                    NewNode.Text = strName
            End Select

            If strName <> "" And NewNode.Text = "" Then
                NewNode.Text = strName
            End If
        Catch ex As Exception
            NewNode.Text = strName
        End Try

         NewNode.Value = strMID

        If strURL <> "" Then
            If IsNewWin.ToUpper = "TRUE" Then
                NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','Y');"
            Else
                NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','N');"
            End If
        Else
            NewNode.NavigateUrl = "javascript:RedirectPage('','N')"
        End If

        subItem.ChildNodes.Add(NewNode)
        BuildLevel3Tree(strMID, NewNode) ''遞迴''可再做第4.5...層

    End Sub


    ''' <summary>
    ''' 第1.2層的點
    ''' </summary>
    ''' <param name="strMID"></param>
    ''' <param name="strName"></param>
    ''' <param name="strURL"></param>
    ''' <param name="intLevel11"></param>
    ''' <param name="ChnName"></param>
    ''' <param name="EngName"></param>
    ''' <param name="IsNewWin"></param>
    ''' <remarks></remarks>
    Private Sub AddNewNode(ByVal strMID As String, ByVal strName As String, ByVal strURL As String, ByVal intLevel11 As Integer, ByVal ChnName As String, ByVal EngName As String, ByVal IsNewWin As String)
        Dim NewNode As TreeNode
        Dim tmpTitle As String = strName
        NewNode = New TreeNode
        Try
            Select Case (GetUserLanguage().ToLower)
                Case "en-us"
                    NewNode.Text = EngName
                Case "zh-cn"
                    NewNode.Text = ChnName
                Case Else
                    NewNode.Text = strName
            End Select


            If strName <> "" And NewNode.Text = "" Then
                NewNode.Text = strName
            End If
        Catch ex As Exception
            NewNode.Text = strName
        End Try
        NewNode.Value = strMID
        If intLevel11 = 0 Then
            If strMID = "1" Then
                NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            Else
                NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            End If
            If strURL <> "" Then
                If IsNewWin.ToUpper = "TRUE" Then
                    NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','Y');"
                Else
                    NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','N');"
                End If

            Else
                NewNode.NavigateUrl = "javascript:RedirectPage('','N')"
            End If

            ' NewNode.Target = "fraMain"

            Me.TreeView1.Nodes.Add(NewNode)
        Else
            If strURL <> "" Then
                If IsNewWin.ToUpper = "TRUE" Then
                    NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','Y');"
                Else
                    NewNode.NavigateUrl = "javascript:RedirectPage('" & Me.ResolveUrl(strURL) & "','N');"
                End If

            Else
                NewNode.NavigateUrl = "javascript:RedirectPage('','N')"
            End If
            NewNode.SelectAction = TreeNodeSelectAction.SelectExpand
            Me.TreeView1.Nodes.Item(intLevel).ChildNodes.Add(NewNode)
            BuildLevel3Tree(strMID, NewNode)
        End If

    End Sub




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim tmp As String = UID
        Dim cssSt As String = "crtCSS('" + Me.ResolveUrl("~/css/" + GetTheme() + "/WebForm.css") + "');"
        ScriptManager.RegisterStartupScript(Me.Page, GetType(Page), "crtCss", cssSt, True)


        If Not IsPostBack Then
            If GetWinTargetBlank().Trim.ToUpper = "TRUE" Then
                txtWinTargetBlank.Text = "Y"
            Else
                txtWinTargetBlank.Text = "N"
            End If

            If GetMenuCollapsible().Trim.ToUpper = "TRUE" Then
                txtMenuCollapsible.Text = "Y"
            Else
                txtMenuCollapsible.Text = "N"
            End If
        End If
        Dim stJava As String
        If Not IsPostBack Then
            If Not IsNothing(Session("myURL")) Then

                stJava = "<script language=JavaScript>  RedirectPage('" & Session("myURL") & "','N');</script>"
                Page.ClientScript.RegisterClientScriptBlock(GetType(WebForm_Main_Menu), "returnUrl", stJava)
                Session.Remove("myURL")
            End If
        End If
        If Page.IsPostBack = False Then

            BuildTreeView()
            TreeView1.CollapseAll() '縮起來
        Else

        End If
    End Sub




   
End Class
